﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleRest.Core.Routing;
using SimpleRest.Core.Routing;
using SimpleRest.Core;
using SimpleRest.WebTest.Services;

namespace SimpleRest.WebTest.Services
{
    [Service("reviews", 1)]
    public class MockReviewsService : ServiceBase
    {
        private List<Review> Items = new List<Review>()
        {
            Review.GetMock("0"),
            Review.GetMock("1"),
            Review.GetMock("2"),
            Review.GetMock("3"),
            Review.GetMock("4"),
            Review.GetMock("5"),
            Review.GetMock("6"),
            Review.GetMock("7"),
            Review.GetMock("8"),
            Review.GetMock("9"),
            Review.GetMock("10"),
            Review.GetMock("11"),
            Review.GetMock("12"),
            Review.GetMock("13"),
            Review.GetMock("14"),
            Review.GetMock("15"),
            Review.GetMock("16"),
            Review.GetMock("17"),
            Review.GetMock("18"),
            Review.GetMock("19"),
        };

        [WebGet(UriTemplate = "?entity_id={entityId}&min={min}&max={max}&user_id={userId}&sort={sort}&startdt={startDt}&enddt={endDt}")]
        public OperationResponse<List<Review>> GetAll(string entityId, int min, int max, string userId, string sort, DateTime startDt, DateTime endDt)
        {
            return new OperationResponse<List<Review>>(Items);
        }

        [WebGet(UriTemplate = "{id}")]
        public OperationResponse<Review> Get(string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return new OperationResponse<Review>( System.Net.HttpStatusCode.NotFound);
            return new OperationResponse<Review>(item);
        }

        /// <summary>
        /// Hi this is commented
        /// </summary>
        /// <param name="review"></param>
        /// <returns></returns>
        [WebPost(UriTemplate = "")]
        public OperationResponse<string> Create([HttpBody]Review review)
        {
            review.Id = Guid.NewGuid().ToString();
            Items.Add(review);
            return new OperationResponse<string>(review.Id, System.Net.HttpStatusCode.Created);
        }

        [WebDelete(UriTemplate = "{id}")]
        public OperationResponse Delete(string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return new OperationResponse(System.Net.HttpStatusCode.NotImplemented);

            Items.Remove(item);
            return new OperationResponse(System.Net.HttpStatusCode.OK);
        }

        [WebPut(UriTemplate = "{id}")]
        public OperationResponse Update([HttpBody]Review review, string id)
        {
            var item = Items.FirstOrDefault(d => d.Id == id);
            if (item == null)
                return new OperationResponse(System.Net.HttpStatusCode.NotFound);

            review.Id = id;
            Items.Remove(item);
            Items.Add(review);
            return new OperationResponse(System.Net.HttpStatusCode.OK);
        }
    }
}